package git.matrix.controller;

import git.matrix.pojo.User;
import git.matrix.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;

@Controller
public class LoginController {
    @Autowired
    UserService userService ;

    /**
     * 跳转登录页面
     * @return 请求转发到WEB-INF/jsp/login.js
     */
    @RequestMapping(value = "login",method = RequestMethod.GET )
    public String getLogin(){
        return "login";
    }

    /**
     * 登录提交,
     * @return
     */
    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    public String login(HttpSession session ,
                        @RequestParam String userName,
                        @RequestParam String password){
        System.out.println("获取到的数据:"+userName+password);
        User login = userService.getLogin(userName,password);
        if (login!=null){
            //登录成功
            session.setAttribute("userOnLogin",login); //存入用户信息
            session.removeAttribute("error");
            return "redirect:/frame"; //转发到首页
        }
        //登录失败
        session.setAttribute("error","你输入的用户名或密码错误!");
        session.removeAttribute("successSavePwd");
        return "redirect:login";
    }


    /**
     * 主页面
     */
    @RequestMapping("/frame")
    public String doFound(){
        return "frame";
    }

    //退出登录
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("userOnLogin");
        session.removeAttribute("successSavePwd");
        return "redirect:login";
    }


}
